
package com.wwy.monitor.service.acl;

import com.alibaba.buc.acl.api.output.check.CheckActionsResult;
import com.alibaba.buc.api.exception.BucException;
import com.wwy.monitor.model.vo.AclCheckActionResult;
import com.wwy.monitor.service.acl.helper.UrlCheckServiceHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author wanbiaoge
 * @date 2020.12.31
 */
@Service("acl_UrlCheckService")
public class UrlCheckService {
    @Autowired
    private AclInfo aclInfo;
    @Autowired
    private UrlCheckServiceHelper urlCheckServiceHelper;

    /**
     * è°ç¨ACLæ¥å£æ§è¡URLæééªè¯
     *
     * @param userId   BUC ç¨æ·id éç¨æ·å·¥å·
     * @param pathName è¢«æ£æ¥çurl
     * @return List<CheckActionsResult>
     */
    public AclCheckActionResult checkActions(Integer userId, String pathName) throws BucException {
        Boolean checkResult = false;
        String permissonName = "";
        CheckActionsResult checkActionsResult = urlCheckServiceHelper.doCheckAccessControle(userId, pathName);
        if (!Objects.isNull(checkActionsResult)) {
            List<CheckActionsResult.CheckActionResultInner> innerList =
                    checkActionsResult.getCheckActionResults();
            for (CheckActionsResult.CheckActionResultInner checkPermissionResult : innerList) {
                checkResult = checkPermissionResult.isAccessible();
                permissonName = checkPermissionResult.getPermissionName();
                if (!checkResult) {
                    break;
                }
            }
        }
        AclCheckActionResult aclCheckActionResult = new AclCheckActionResult();
        aclCheckActionResult.setAccessible(checkResult);
        aclCheckActionResult.setPermissionName(permissonName);
        aclCheckActionResult.setApplyUrl(aclInfo.getPermissionApplyUrl());
        return aclCheckActionResult;
    }
}
